package cn.yyh.serviceProduct.controller;

import cn.yyh.commons.exception.MyException;
import cn.yyh.commons.utils.GetTokenUtil;
import cn.yyh.commons.utils.PageInfo;
import cn.yyh.commons.utils.R;
import cn.yyh.serviceProduct.domain.Product;
import cn.yyh.serviceProduct.domain.ProductDetail;
import cn.yyh.serviceProduct.mapper.ProductDetailMapper;
import cn.yyh.serviceProduct.query.ProductQuery;
import cn.yyh.serviceProduct.service.ProductService;
import cn.yyh.serviceProduct.util.ContextUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @author 杨永航
 * @version 1.0
 * @Title: ProductController
 * @Package: cn.yyh.serviceProduct.controller
 * @date 2021/7/12
 */
@RestController
@RequestMapping("/serviceProduct/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductDetailMapper detailMapper;

    @ApiOperation(value = "根据ID查询")
    @GetMapping("aaaaa")
    public void aaa() {

        Long productId = 170L;
        Product product = productService.getById(productId);
        ProductDetail productDetail = detailMapper.getByProductId(productId);

        product.setId(null);
        productDetail.setId(null);
        productDetail.setProduct_id(null);
        product.setDetail(productDetail);

        for (int i = 0; i < 100; i++) {
            if (i%3==0){
                product.setName("基础版洗澡"+i);
                product.setCostprice(new BigDecimal(30).toString());
                product.setSaleprice(new BigDecimal(50));
                product.setSalecount(50L+i);
            }
            else if(i%3==1){
                product.setName("中级版洗澡"+i);
                product.setCostprice(new BigDecimal(50).toString());
                product.setSaleprice(new BigDecimal(80));
                product.setSalecount(30L+i);
            }
            else{
                product.setName("高级版洗澡"+i);
                product.setCostprice(new BigDecimal(100).toString());
                product.setSaleprice(new BigDecimal(150));
                product.setSalecount(10L+i);
            }

            productService.save(product);
        }
    }

    @ApiOperation(value = "高级查询,并分页")
    @PostMapping("pageQuery")
    public R pageQuery(
            @ApiParam(name = "query",value = "高级查询")
            @RequestBody(required = false) ProductQuery query, HttpServletRequest request) {

        try {
            PageInfo<Product> pageInfo = productService.pageQuery(query,
                    Objects.requireNonNull(ContextUtil.getLoginInfo(request)).getId(),
                    GetTokenUtil.getToken(request));
            return R.oK().data("totals", pageInfo.getTotals()).data("rows",pageInfo.getRows());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "根据ID查询")
    @GetMapping("{id}")
    public R getById(
            @ApiParam(name = "id",value = "部门编号",required = true)
            @PathVariable Long id) {

        try {
            Product byId = productService.getById(id);
            return R.oK().data("rows", byId);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "查询所有")
    @GetMapping("listAll")
    public R listAll() {

        try {
            List<Product> list = productService.listAll();
            return R.oK().data("rows", list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "根据有没有ID判断是添加还是修改")
    @PostMapping("save")
    public R saveOrUpdate(
            @ApiParam(name = "department",value = "数据封装对象")
            @RequestBody Product department,HttpServletRequest request) {

        try {
            if (department.getId() == null) {
                productService.save(department,
                        Objects.requireNonNull(ContextUtil.getLoginInfo(request)).getId(),
                        GetTokenUtil.getToken(request));
            }else {
                productService.update(department,
                        Objects.requireNonNull(ContextUtil.getLoginInfo(request)).getId(),
                        GetTokenUtil.getToken(request));
            }
            return R.oK();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "根据ID删除")
    @DeleteMapping("{id}")
    public R delete(
            @ApiParam(name = "id",value = "编号",required = true)
            @PathVariable Long id){

        try {
            productService.delete(id);
            return R.oK();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }


    @ApiOperation(value = "根据id批量删除")
    @PostMapping("/deleteBatch")
    public R deleteBatch(
            @ApiParam(name = "ids",value = "编号集合",required = true)
            @RequestBody List<Long> ids){

        try {
            productService.deleteBatch(ids);
            return R.oK();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @PutMapping("/onsale")
    public R onSalePet(@RequestBody List<Long> ids){
        try {
            productService.onSale(ids);
            return R.oK();
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }

    @PutMapping("/offsale")
    public R offSalePet(@RequestBody List<Long> ids){
        try {
            productService.offSale(ids);
            return R.oK();
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }

}
